package cn.yunhe.service.impl;

import cn.yunhe.mapper.DeptPMapper;
import cn.yunhe.mapper.UserpMapper;
import cn.yunhe.pojo.*;
import cn.yunhe.service.DeptService;
import cn.yunhe.vo.DeptVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service("deptService")
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptPMapper deptPMapper;

    @Autowired
    private UserpMapper userPMapper;

    @Override
    public PageBean listDeptOfPage(PageBean pageBean) {


        int start = (pageBean.getCurPage() - 1) * pageBean.getPageSize();

        // 设置分页sql 条件
        PageHelper.offsetPage(start,pageBean.getPageSize());

        List<DeptVo> deptPVoList =  deptPMapper.listDeptAndParent();

        // 分页插件 类 PageInfo
        PageInfo<DeptVo> pageInfo = new PageInfo<>(deptPVoList);

        pageBean.setDatas(pageInfo.getList());  // 分装分页数据
        pageBean.setTotalPages(pageInfo.getPages());  // 共多少页

        pageBean.setTotalRows(pageInfo.getTotal());


        return pageBean;
    }

    @Override
    public List<DeptP> listDept() {

        return deptPMapper.selectByExample(null);
    }

    @Override
    public int addDept(DeptP deptP) {

        deptP.setDeptId(UUID.randomUUID().toString());
        deptP.setState(1);
        deptP.setDeptNo(String.valueOf(System.currentTimeMillis()));
        return deptPMapper.insertSelective(deptP);

    }

    @Override
    public DeptVo deptSelectByID(String deptId) {

        return deptPMapper.deptSelectByID(deptId);
    }

    @Override
    public void deleteDeptVoById(String id) {
        deptPMapper.deleteDeptVoById(id);
    }

    @Override
    public void updateByDeptVo(String parentId, String deptName, String state) {
        deptPMapper.updateByDeptVo(parentId,deptName,state);
    }

    @Override
    public int saveUpdate(DeptP deptP) {
       return deptPMapper.updateByPrimaryKeySelective(deptP);
    }

    @Override
    public GllsResult deleteDeptByDeptId(String[] deptIds) {
        // 不能删除的部门的名字的集合
        ArrayList<String> deptNames = new ArrayList<>();
        for(int i=0;i<deptIds.length;i++){
            String deptId = deptIds[i];
            // 判断该部门是否有子部门 如果有则不能删除
            if(isHaveChildDept(deptId)){
                DeptP deptP = deptPMapper.selectByPrimaryKey(deptId);
                deptNames.add(deptP.getDeptName());
                continue;
            }

            // 判断 该部门是否员工  如果有 不能删除
            if(isHaveUser(deptId)){
                DeptP deptP = deptPMapper.selectByPrimaryKey(deptId);
                deptNames.add(deptP.getDeptName());
                continue;
            }

            deptPMapper.deleteByPrimaryKey(deptId);
        }

        if(deptNames.size()==0){
            return new GllsResult(200,null,"删除成功");
        }
        return new GllsResult(400,deptNames,"部分未删除成功");
    }

    @Override
    public List<DeptP> selectBydept() {


        return  deptPMapper.selectBydept();
    }

    private boolean isHaveUser(String deptId) {
        UserpExample userPExample = new UserpExample();
        UserpExample.Criteria criteria = userPExample.createCriteria();
        criteria.andDeptIdEqualTo(deptId);
        List<Userp> userPS = userPMapper.selectByExample(userPExample);
        System.out.println("这是用户的"+userPS);
        if(userPS!=null && userPS.size()>0){
            return true;
        }
        return false;
    }

    private boolean isHaveChildDept(String deptId) {
        DeptPExample deptPExample = new DeptPExample();
        DeptPExample.Criteria criteria = deptPExample.createCriteria();
        criteria.andParentIdEqualTo(deptId);
        List<DeptP> deptPList = deptPMapper.selectByExample(deptPExample);
        System.out.println("这是子部门的"+deptPList);
        if(deptPList!=null&&deptPList.size()>0){
            return true;
        }
        return false;
    }


}
